<!DOCTYPE html>
<html lang="en">
<!--头部信息-->
<head>
    <meta charset="utf-8">
    <title>生薄管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">

    <script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
</head>
<!--生薄管理-->
<body>
<div class="layuimini-container">
    <div class="layuimini-main">


        <!--搜索框-->
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <div class="layui-form layui-form-pane">
                    <div class="layui-form-item common" id="seach">


                        <!--姓名-->
                        <div class="layui-inline">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" id="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>


                        <!--类别-->
                        <div class="layui-inline">
                            <label class="layui-form-label required">类别</label>
                            <div class="layui-input-block" style="width: 65%">
                                <select name="type" id="type" lay-filter="type">
                                    <option value="" disabled="disabled">请选择</option>
                                </select>
                            </div>
                        </div>


                        <!--搜索按钮-->
                        <div class="layui-inline" style="padding-left: 5px">
                            <button class="layui-btn layui-btn-primary" data-type="reload" id="search"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>


                    </div>
                </div>
            </div>
        </fieldset>

        <!--表头操作-->
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"><i
                        class="layui-icon layui-icon-delete"></i>批量删除
                </button>
            </div>
        </script>


        <!--创建表格-->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


        <!--表内操作-->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>


    </div>
</div>
<script>
    layui.use(['form', 'table', 'jquery'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        getType();

        /*初始化【类别】下拉列表框*/
        function getType() {
            $.ajax({
                type: "GET",
                url: "/getTypeList",
                dataType: "json",
                success: function (data) {
                    for (var i = 0; i < data.length; i++) {
                        $("#type").append("<option value=\"" + data[i].typeId + "\">" + data[i].typeName +
                            "</option>");
                        $("#type").val("");
                    }
                    layui.form.render("select");
                }
            });
        };


        /*表格数据渲染*/
        table.render({
            elem: '#currentTableId',
            url: '/getLivebookList',
            toolbar: '#toolbarDemo',
            cellMinWidth: 50,
            height: 440,
            limit: 8,
            limits: [8, 16, 24, 32, 40],
            method: 'GET',
            text: {
                none: '暂时没有数据哦！'
            },
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '生薄所有数据',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {type: "checkbox", width: 50, align: "center"}
                    , {type: 'numbers', width: 50, title: '序号', align: "center"}
                    , {field: 'id', hide: true, width: 80, title: 'ID', sort: true, align: "center"}
                    , {field: 'username', title: '姓名', align: "center"}
                    , {field: 'sex', title: '性别', align: "center"}
                    , {
                    field: 'totalAge', title: '当前寿命', sort: true, align: "center", templet: function (d) {
                        return d.totalAge += "岁";
                    }
                }
                    , {
                    field: 'overAge', title: '剩余寿命', sort: true, align: "center", templet: function (d) {
                        return d.overAge += "年";
                    }
                }
                    , {field: 'type', title: '类别', align: "center"}
                    , {field: 'lass_updatetime', width: 200, title: '最后编辑时间', sort: true, align: "center"}
                    , {fixed: 'right', title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                ]
            ],
            page: true,
            id: "testId"
        });


        /*搜索事件处理*/
        $('#search').click(function () {
            var username = $('#username').val();
            var type = $("#type").val();
            /*表格重载*/
            table.reload('testId', {
                page: {
                    curr: 1
                },
                where: {
                    username: username,
                    type: type
                }
            }, 'data');
        });


        /*表头按钮事件处理*/
        table.on('toolbar(currentTableFilter)', function (obj) {
            //批量删除
            if (obj.event === 'delete') {
                var checkStatus = table.checkStatus('testId');
                /*判断是否选中数据*/
                if (checkStatus.data.length === 0) {
                    parent.layer.msg('请先选择要删除的数据！', {icon: 2});
                    return;
                }
                layer.confirm('确定要删除选中的用户吗？', {icon: 3}, function (index) {
                    layer.close(index);
                    index = layer.load(1, {shade: [0.1, '#fff']});
                    //存放要删除的ID
                    var ids = '';
                    for (var i = 0; i < checkStatus.data.length; i++) {
                        ids += checkStatus.data[i].id + ",";
                    }
                    parent.layer.msg('删除中...', {icon: 16, shade: 0.3, time: 5000});
                    //删除请求
                    $.ajax({
                        type: 'DELETE',
                        data: {ids: ids},
                        url: '/batchDelLive',
                        error: function () {
                            layer.alert("系统错误！");
                        },
                        success: function (result) {
                            layer.closeAll('loading');
                            if (result === 'success') {
                                parent.layer.msg('删除成功！', {
                                    icon: 1, time: 2000, shade: 0.2, end: function () {
                                        window.location.reload();
                                    }
                                });
                                layer.close(index);
                            } else {
                                parent.layer.msg('删除失败!', {
                                    icon: 5, time: 1000, end: function () {
                                        window.location.reload()
                                    }
                                });
                                layer.close(index);
                            }
                        }
                    });
                });
            }
        });


        /*表格复选框处理*/
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });


        /*表内操作事件处理*/
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '用户信息',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['80%', '90%'],
                    content: '/livebook-edit',
                    success: function (layero, index) {
                        $.ajax({
                            type: 'GET',
                            url: '/getUserlifeById/' + data.id,
                            dataType: 'text',
                            error: function () {
                                layer.alert('系统错误！');
                            },
                            success: function (result) {
                                var body = layer.getChildFrame('body', index);
                                //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                                var iframeWin = window[layero.find('iframe')[0]['name']];
                                body.find('#id label').text(data.id);
                                body.find('#username').val(data.username);
                                body.find('#sex').val(data.sex);
                                body.find('#ageCurrent').val(data.totalAge);
                                body.find('#ageOver').val(data.overAge);
                                body.find('#types').val(result);
                                body.find('#lass_updatetime label').text(data.lass_updatetime);
                            }
                        });
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除该用户吗？', {icon: 3}, function (index) {
                    /*删除请求*/
                    $.ajax({
                        type: 'DELETE',
                        url: '/deleteLivebookByid/' + data.id,
                        dataType: 'text',
                        error: function () {
                            layer.alert('系统错误！');
                        },
                        success: function (result) {
                            if (result === "success") {
                                layer.alert('删除成功！',
                                    {
                                        title: '成功',
                                        skin: 'layui-layer-molv',
                                        btn: ['返回'],
                                        anim: 1,
                                        icon: 6,
                                        yes: function (index, layero) {
                                            obj.del();
                                            table.reload('testId', {
                                                page: {
                                                    curr: 1
                                                },
                                            });
                                            layer.close(index);
                                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                                        }
                                    });
                            } else {
                                layer.alert('删除失败，继续吗？',
                                    {
                                        title: '错误',
                                        skin: 'layui-layer-molv',
                                        btn: ['重试'],
                                        anim: 1,
                                        icon: 5,
                                        yes: function (index) {
                                            layer.close(index);
                                        }
                                    });
                            }
                        }
                    });
                });
            }
        });
    });
</script>

</body>
</html>